﻿#include "ScalarBarSetting.h"
#include "ui_ScalarBarSetting.h"

#include <QStandardItem>
#include <QStandardItemModel>
#include <QLineEdit>
#include <QComboBox>
#include <QCheckBox>
#include <QStyledItemDelegate>
#include <QTableWidgetItem>
#include <QDebug>
#include "BaseWidget/DoubleSpinBox.h"
#include "BaseWidget/EColorComboBox.h"
#include "IdaWidgetHelper.h"

namespace pst
{
    ScalarBarSetting::ScalarBarSetting(QWidget* parent)
        : QWidget(parent)
        , ui(new Ui::ScalarBarSetting())
        , titleTextEdit(new QLineEdit(this))
        //, comboBoxFontType(new QComboBox(this))
        , comboBoxFontSize(new QComboBox(this))
        , comboBoxFontColor(new BaseWidget::EColorComboBox(this))
        , checkBoxAutoRange(new QCheckBox(this))
        , checkBoxShowTitle(new QCheckBox(this))
        , dSpinBoxMaxValue(new BaseWidget::DoubleSpinBox(this))
        , dSpinBoxMinValue(new BaseWidget::DoubleSpinBox(this))
        , m_rowCount(0)
        , m_scalarBarMinValueOriginal{ 0. }
        , m_scalarBarMaxValueOriginal{ 0. }
    {
        ui->setupUi(this);

        auto tableWidget = ui->tableWidget;
        tableWidget->setRowCount(7);
        tableWidget->setColumnCount(2);
        tableWidget->setHorizontalHeaderLabels(QStringList() << "名称" << "值");
        tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

        tableWidget->setItem(m_rowCount, 0, new QTableWidgetItem("自动"));
        tableWidget->setCellWidget(m_rowCount, 1, checkBoxAutoRange);

        tableWidget->setItem(++m_rowCount, 0, new QTableWidgetItem("最大值"));
        dSpinBoxMaxValue->setRange(-1e15, 1e15);
        tableWidget->setCellWidget(m_rowCount, 1, dSpinBoxMaxValue);
        dSpinBoxMaxValue->setValue(1.0);
        dSpinBoxMaxValue->setDecimals(15);

        tableWidget->setItem(++m_rowCount, 0, new QTableWidgetItem("最小值"));
        dSpinBoxMinValue->setRange(-1e15, 1e15);
        tableWidget->setCellWidget(m_rowCount, 1, dSpinBoxMinValue);
        dSpinBoxMinValue->setValue(0.0);
        dSpinBoxMinValue->setDecimals(15);

        connect(dSpinBoxMaxValue, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, [=](double value){ 
            const double minAbs = 1e-15; 
            const double maxAbs = 1e+15; 
            // 检查值是否在有效范围外 
            if (qAbs(value) > 0 && qAbs(value) < minAbs) { 
            // 修正为0或边界值（根据需求选择） 
                dSpinBoxMaxValue->setValue(0.0); // 或 (value > 0 ? minAbs : -minAbs) 
            }
            // 检查值是否在有效范围外 
            if (value > maxAbs) { 
                dSpinBoxMaxValue->setValue(1e+15);
            }
            if (value < -maxAbs) { 
                dSpinBoxMaxValue->setValue(-1e+15);
            }
        }); 
        connect(dSpinBoxMinValue, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, [=](double value){ 
            const double minAbs = 1e-15; 
            const double maxAbs = 1e+15;
            // 检查值是否在有效范围外 
            if (qAbs(value) > 0 && qAbs(value) < minAbs) { 
            // 修正为0或边界值（根据需求选择） 
                dSpinBoxMinValue->setValue(0.0); // 或 (value > 0 ? minAbs : -minAbs) 
            } 
            // 检查值是否在有效范围外 
            if (value > maxAbs) { 
                dSpinBoxMinValue->setValue(1e+15);
            }
            if (value < -maxAbs) { 
                dSpinBoxMinValue->setValue(-1e+15);
            }
        }); 

        /*
         * checkbox 控制禁用状态
         */
        connect(checkBoxAutoRange, &QCheckBox::toggled, this,
                [=](bool checked)
                {
                    dSpinBoxMaxValue->setEnabled(!checked);
                    dSpinBoxMinValue->setEnabled(!checked);
                });

        tableWidget->setItem(++m_rowCount, 0, new QTableWidgetItem("显示标题"));
        tableWidget->setCellWidget(m_rowCount, 1, checkBoxShowTitle);

        tableWidget->setItem(++m_rowCount, 0, new QTableWidgetItem("标题"));
        tableWidget->setCellWidget(m_rowCount, 1, titleTextEdit);

        //tableWidget->setItem(++m_rowCount, 0, new QTableWidgetItem("字体"));
        //comboBoxFontType->addItem("宋体");
        //comboBoxFontType->addItem("微软雅黑");
        //tableWidget->setCellWidget(m_rowCount, 1, comboBoxFontType);

        tableWidget->setItem(++m_rowCount, 0, new QTableWidgetItem("字体颜色"));
        comboBoxFontColor->appendPredefinedColors();
        tableWidget->setCellWidget(m_rowCount, 1, comboBoxFontColor);


        tableWidget->setItem(++m_rowCount, 0, new QTableWidgetItem("字号"));
        for (int i = 1; i <= 50; ++i)
        {
            comboBoxFontSize->addItem(QString::number(i));
        }
        tableWidget->setCellWidget(m_rowCount, 1, comboBoxFontSize);

        //设置第一列不可编辑
        for (int row = 0; row < tableWidget->rowCount(); ++row)
        {
            QTableWidgetItem* item = tableWidget->item(row, 0);
            item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        }
        IdaWidgetHelperInstance->setTableWidgetStyle(tableWidget);
    }

    ScalarBarSetting::~ScalarBarSetting()
    {
        delete ui;
    }

    void ScalarBarSetting::setTitle(const QString& title)
    {
        titleTextEdit->setText(title);
    }

    QString pst::ScalarBarSetting::getTitle() const
    {
        return titleTextEdit->text();
    }

    //void ScalarBarSetting::setFontFamily(const QString& fontFamily)
    //{
    //    comboBoxFontType->setCurrentText(fontFamily);
    //}

    //QString ScalarBarSetting::getFontFamily() const
    //{
    //    return comboBoxFontType->currentText();
    //}

    void ScalarBarSetting::setFontSize(int fontSize)
    {
        comboBoxFontSize->setCurrentText(QString::number(fontSize));
    }

    int ScalarBarSetting::getFontSize() const
    {
        return comboBoxFontSize->currentText().toInt();
    }

    void ScalarBarSetting::setMaxValue(double value)
    {
        dSpinBoxMaxValue->setValue(value);
    }

    double ScalarBarSetting::getMaxValue() const
    {
        return dSpinBoxMaxValue->value();
    }

    void ScalarBarSetting::setMinValue(double value)
    {
        dSpinBoxMinValue->setValue(value);
    }

    double pst::ScalarBarSetting::getMinValue() const
    {
        return dSpinBoxMinValue->value();
    }

    void ScalarBarSetting::setUseScalarBarAutoRange(bool isAuto)
    {
        checkBoxAutoRange->setChecked(isAuto);
    }

    bool ScalarBarSetting::getUseScalarBarAutoRange() const
    {
        return checkBoxAutoRange->isChecked();
    }

    void ScalarBarSetting::setIsShowTitle(bool isshow)
    {
        checkBoxShowTitle->setChecked(isshow);
    }

    bool ScalarBarSetting::getIsShowTitle() const
    {
        return checkBoxShowTitle->isChecked();
    }

    void ScalarBarSetting::setTitleColor(const QColor& color)
    {
        comboBoxFontColor->setCurrentColor(color);
    }

    QColor ScalarBarSetting::getTitleColor() const
    {
        return comboBoxFontColor->currentColor();
    }

    void ScalarBarSetting::setOriginalScalarBarMinRange(double minValue)
    {
        m_scalarBarMinValueOriginal = minValue;
    }

    double ScalarBarSetting::getOriginalScalarBarMinRange() const
    {
        return m_scalarBarMinValueOriginal;
    }

    void ScalarBarSetting::setOriginalScalarBarMaxRange(double maxValue)
    {
        m_scalarBarMaxValueOriginal = maxValue;
    }

    double ScalarBarSetting::getOriginalScalarBarMaxRange() const
    {
        return m_scalarBarMaxValueOriginal;
    }

}
